﻿using DevExpress.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Windows.Forms;

namespace XCI.Test
{
    public partial class Form1 : FrmBase
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void setGalleryOptions()
        {
            //Base gallery options
            galleryControl1.Gallery.ShowItemText = true;
            galleryControl1.Gallery.ImageSize = new Size(200, 200);
            galleryControl1.Gallery.ItemCheckMode = DevExpress.XtraBars.Ribbon.Gallery.ItemCheckMode.Multiple;
            galleryControl1.Gallery.ItemImageLayout = DevExpress.Utils.Drawing.ImageLayoutMode.ZoomInside;
            //Image load options
            galleryControl1.Gallery.OptionsImageLoad.AsyncLoad = true;
            galleryControl1.Gallery.OptionsImageLoad.AnimationType = DevExpress.Utils.ImageContentAnimationType.Expand;
            galleryControl1.Gallery.OptionsImageLoad.DesiredThumbnailSize = new Size(200, 200);
        }

        private void createItems()
        {
            var files = IOHelper.GetFiles(@"C:\Users\lvyanyang\Pictures\rrr");
            for (int row = 0; row < files.Length; row++)
            {
                var itemName = Path.GetFileNameWithoutExtension(files[row]);
                var item = new DevExpress.XtraBars.Ribbon.GalleryItem(null, itemName, null);
                item.Tag = files[row];
                galleryControl1.Gallery.Groups[0].Items.Add(item);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //var img = ImageHelper.LoadImage(@"C:\Users\lvyanyang\Pictures\Saved Pictures\20121017_173426.JPG");
            //Task.Run(async () =>
            //{
            //    pictureEdit1.LoadAsync(@"C:\Users\lvyanyang\Pictures\Saved Pictures\20121017_173426.JPG");
            //    await Task.Delay(3000);
            //}).ContinueWith(p =>
            //{
            //    //pictureEdit1.Image = img;
            //}, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

            
        }

        private void GalleryControl1_Gallery_GetThumbnailImage(object sender, DevExpress.XtraBars.Ribbon.Gallery.GalleryThumbnailImageEventArgs e)
        {
            var filePath = e.Item.Tag.ToString();
            Image thumbImg = ImageHelper.LoadImage(filePath);
            e.ThumbnailImage = e.CreateThumbnailImage(thumbImg);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            setGalleryOptions();
            createItems();
        }

        //private void GalleryControl1_Gallery_ContextButtonCustomize(object sender, DevExpress.XtraBars.Ribbon.Gallery.GalleryContextButtonCustomizeEventArgs e)
        //{
        //    //e.Item.
        //}

        private void GalleryControl1_Gallery_ContextButtonClick(object sender, DevExpress.Utils.ContextItemClickEventArgs e)
        {
            if (e.Item.Name.Equals("checkContextButton1"))
            {
                var item = (DevExpress.XtraBars.Ribbon.GalleryItem)e.DataItem;
                item.Checked = !item.Checked;
                ((CheckContextButton)e.Item).Checked = item.Checked;
            }
        }
    }
}
